package com.ctshk.rpc.ship.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.TokenUser;
import com.ctshk.rpc.ship.entity.ShipProductSaleRecord;
import com.ctshk.rpc.ship.mapper.ShipProductSaleRecordMapper;
import com.ctshk.rpc.ship.req.ShipProductSaleRecordReq;
import com.ctshk.rpc.ship.service.IShipProductSaleRecordService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

/**
 * <p>
 * 产品库存售卖记录 服务实现类
 * </p>
 *
 * @author 刘蕾
 * @since 2021-03-18
 */
@DubboService
public class ShipProductSaleRecordServiceImpl extends ServiceImpl<ShipProductSaleRecordMapper, ShipProductSaleRecord> implements IShipProductSaleRecordService {
    @Autowired
    private ShipProductSaleRecordMapper shipProductSaleRecordMapper;

    @Override
    public Result insertRecord(ShipProductSaleRecordReq shipProductSaleRecordReq, TokenUser user) {
        ShipProductSaleRecord shipProductSaleRecord = new ShipProductSaleRecord();
        BeanUtils.copyProperties(shipProductSaleRecordReq,shipProductSaleRecord);
        LocalDateTime now = LocalDateTime.now();
        shipProductSaleRecord.setGmtCreate(now);
        shipProductSaleRecord.setGmtModified(now);
        shipProductSaleRecord.setIsDeleted(IsDeletedCode.NO.getCode());
        if(user != null){
            shipProductSaleRecord.setModifiedId(user.getId());
            shipProductSaleRecord.setCreateId(user.getId());
        }
        return Result.success(shipProductSaleRecordMapper.insert(shipProductSaleRecord));
    }

    @Override
    public Result deleteRecord(Long orderId, Long productId) {
        int delete = shipProductSaleRecordMapper.delete(Wrappers.<ShipProductSaleRecord>lambdaQuery().eq(ShipProductSaleRecord::getOrderId, orderId)
                .eq(ShipProductSaleRecord::getProductId, productId));
        return Result.success(delete);
    }
}
